﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

using MINERP.DAL;
using MINERP.DAL.InfoConfig;
using MINERP.MODEL.InfoConfig;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;


namespace MINERP.BLL.InfoConfig
{
    public class ProductBLL
    {
        #region //Private Fields


        #endregion


        #region //Public Properties

        #endregion



        #region //Public Methods
        /// <summary>
        /// Get product list
        /// </summary>
        /// <returns></returns>
        public List<Tb_Product> GetProductList()
        {
            return ProductDAL.GetProductList();
        }

        /// <summary>
        /// Get Product object
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public Tb_Product GetProductObj(int productId)
        {
            return ProductDAL.GetProductObj(productId);
        }

        public Tb_Product GetProductObj(string productName)
        {
            return ProductDAL.GetProductObj(productName);
        }

        /// <summary>
        /// 检查相同产品名称、尺寸的产品是否存在
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public Tb_Product GetProductObj(ProductEntity product)
        {
            using (newSampleYPDBEntities db=new newSampleYPDBEntities ())
            {
                if (product.ProuctId!=0)//修改时检查
                {
                    return db.Tb_Product.FirstOrDefault(p =>p.ProuctId!=product.ProuctId&& p.ProductName == product.ProductName && p.Width == product.Width && p.Height == product.Height);
                }
                else//新添加时检查
                {
                    return db.Tb_Product.FirstOrDefault(p =>p.ProductName == product.ProductName && p.Width == product.Width && p.Height == product.Height);
                }
                
            }
        }
        /// <summary>
        /// Add product
        /// </summary>
        /// <param name="obj"></param>
        public void AddProduct(Tb_Product obj)
        {

            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    ProductDAL.AddProduct(obj);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        public void AddProduct(Tb_Product obj,string PrintName)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    ProductDAL.AddProduct(obj);
                    ProductDAL.AddProductPrintName(obj, PrintName);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        public void AddOutShopCategory(int productid, int outshopid)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                //Tb_Product product = db.Tb_Product.FirstOrDefault(tp => tp.ProuctId == productid);
                //product.t
                OutProcessShop outshop = db.OutProcessShop.FirstOrDefault(p=>p.OutShopId==outshopid);
                if (outshop != null)
                {
                    Tb_OutShop_ProductCategory osp = new Tb_OutShop_ProductCategory();
                    osp.UnitPrice = (decimal)0;
                    osp.NeedTime = 72;
                    osp.IsDefaultOutShop = true;
                    osp.Tb_Product = db.Tb_Product.FirstOrDefault(tb => tb.ProuctId == productid);
                    outshop.Tb_OutShop_ProductCategory.Add(osp);
                    db.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Update Product
        /// </summary>
        /// <param name="obj"></param>
        public void UpdateProduct(Tb_Product obj)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    ProductDAL.UpdateProduct(obj);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
           
        }
        /// <summary>
        /// Update Product
        /// </summary>
        /// <param name="obj"></param>
        public void UpdateProduct(Tb_Product obj,ProductPrint inPP)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    ProductDAL.UpdateProduct(obj, inPP);
                    
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

        }
        /// <summary>
        /// Delete Product
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteProduct(ProductEntity obj)
        {
            Tb_Product product = ProductDAL.GetProductObj(obj.ProuctId);
            product.Status = (int)ProductStatus.Delete;
            ProductDAL.DeleteProduct();
        }

        /// <summary>
        /// 调整价格
        /// </summary>
        /// <param name="productEntity"></param>
        /// <param name="addPagePrice"></param>
        /// <param name="unitPrice"></param>
        public void ChangePrice(ProductEntity productEntity, double addPagePrice, double unitPrice,bool isAddPrice)
        {
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                Tb_Product product = Entities.Tb_Product.FirstOrDefault(p => p.ProuctId == productEntity.ProuctId);
                if (product != null)
                {
                    if (!product.Shop_Product_Price.IsLoaded)
                    {
                        product.Shop_Product_Price.Load();

                    }
                    if (isAddPrice)
                    {
                        product.AppendPrice += addPagePrice;
                        product.UnitPrice += unitPrice;
                        foreach (var item in product.Shop_Product_Price)
                        {
                            item.AppendPrice += (decimal)addPagePrice;
                            item.UnitPrice += (decimal)unitPrice;
                        }
                    }
                    else
                    {
                        product.AppendPrice -= addPagePrice;
                        product.UnitPrice -= unitPrice;
                        foreach (var item in product.Shop_Product_Price)
                        {
                            item.AppendPrice -= (decimal)addPagePrice;
                            item.UnitPrice -= (decimal)unitPrice;
                        }
                    }
                    Entities.SaveChanges();
                }
            }
        }
        public List<OutProcessShop_Info> GetAllOutShop()
        {
            List<OutProcessShop_Info> outList = new List<OutProcessShop_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                List<OutProcessShop> listShop = db.OutProcessShop.ToList();
                foreach (OutProcessShop shop in listShop) 
                {
                    OutProcessShop_Info outShop = new OutProcessShop_Info() {  OutShopId=shop.OutShopId,OutShopName=shop.OutShopName};
                    outList.Add(outShop);
                }
 
            }
            return outList;
        }

        
        /// <summary>
        /// 设置产品尺寸
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="photoSizeList"></param>
        /// <returns></returns>
        public int SetProductSize(int productId, List<PhotoSize_Info> photoSizeList)
        {
            int ret = 0;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Tb_Product product = db.Tb_Product.FirstOrDefault(p=>p.ProuctId ==productId);
                if (product != null)
                {
                    if (!product.Photo_Size.IsLoaded)
                    {
                        product.Photo_Size.Load();
                    }
                    List<Photo_Size> photoSizeInDBList = product.Photo_Size.ToList();
                    foreach (var item in photoSizeInDBList)//删除数据库中存在，但是新输入的尺寸中没有的记录
                    {
                        if (!photoSizeList.Exists(p => p.SizeId == item.SizeId))//数据库中的记录在新输入的尺寸中找不到
                        {
                            db.DeleteObject(item);
                        }
                    }

                    foreach (var item in photoSizeList)//添加和更新新的尺寸
                    {
                        if (item.SizeId > 0)//需要更新的记录
                        {
                            Photo_Size ps = db.Photo_Size.FirstOrDefault(p => p.SizeId == item.SizeId);
                            if (ps != null)
                            {
                                ps.Describe = item.Describe;
                                ps.HeightMAX = item.HeightMAX;
                                ps.HeightMIN = item.HeightMIN;
                                ps.WidthMAX = item.WidthMAX;
                                ps.WidthMIN = item.WidthMIN;
                                ps.PageNum = item.PageNum;
                                
                            }
                        }
                        else//需要添加的记录
                        {
                            Photo_Size ps = new Photo_Size
                            {
                                Describe = item.Describe,
                                HeightMAX = item.HeightMAX,
                                HeightMIN = item.HeightMIN,
                                PageNum = item.PageNum,
                                WidthMAX = item.WidthMAX,
                                WidthMIN = item.WidthMIN
                            };
                            product.Photo_Size.Add(ps);
                        }
                    }
                }
                else
                {
                    return 0;//产品不存在
                }
                db.SaveChanges();
                ret = 1;
            }
            return ret;
        }


        /// <summary>
        /// 通过Id获取产品信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ProductEntity GetProductInfoById(int productId)
        {
            ProductEntity productEntity;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Tb_Product product = db.Tb_Product.FirstOrDefault(p=>p.ProuctId==productId);

                if (!product.paperFaceReference.IsLoaded)
                {
                    product.paperFaceReference.Load();
                }
                if (!product.paperTypeReference.IsLoaded)
                {
                    product.paperTypeReference.Load();
                }
                if (!product.printerReference.IsLoaded)
                {
                    product.printerReference.Load();
                }
                if (!product.Tb_PorductCategoryReference.IsLoaded)
                {
                    product.Tb_PorductCategoryReference.Load();
                }
                if (!product.tb_ProductTypeReference.IsLoaded)
                {
                    product.tb_ProductTypeReference.Load();
                }
                if (!product.tb_CostTypeReference.IsLoaded)
                {
                    product.tb_CostTypeReference.Load();
                }
                if (!product.ProductPrintReference.IsLoaded)
                {
                    product.ProductPrintReference.Load();
                }
                if (!product.Photo_Size.IsLoaded)
                {
                    product.Photo_Size.Load();
                }
                productEntity = new ProductEntity();
                productEntity.Height = product.Height;
                productEntity.PageNum = product.PageNum;
                productEntity.MnemonicCode = product.mnemonicCode;
                productEntity.PaperFaceID = product.paperFace.PaperFaceID;
                productEntity.PaperFaceName = product.paperFace.Name;
                productEntity.PaperTypeID = product.paperType.PaperTypeID;
                productEntity.PaperTypeName = product.paperType.Name;
                productEntity.PrinterName = product.printer == null ? "" : product.printer.PName;
                productEntity.ProductCategoryId = product.Tb_PorductCategory.ProductCategoryId;
                productEntity.ProductCategoryName = product.Tb_PorductCategory.ProductCategoryName;
                productEntity.ProductTypeName = product.tb_ProductType.TypeName;
                productEntity.CostTypeName = product.tb_CostType.TypeName;
                productEntity.ProductCode = product.ProductCode;
                productEntity.ProductDes = product.ProductDes;
                productEntity.ProductName = product.ProductName;
                productEntity.UnitPrice = product.UnitPrice;
                productEntity.AppendPrice = product.AppendPrice;
                productEntity.AreaPrice = product.AreaPrice;
                productEntity.ProuctId = product.ProuctId;
                productEntity.Width = product.Width;
                productEntity.CoverHeigth = product.CoverHeight;
                productEntity.CoverWidth = product.CoverWidth;
                productEntity.IsRendered = product.IsRendered;
                productEntity.IsComplex = product.IsComplex;
                productEntity.photoSizeInfoList = new List<PhotoSize_Info>();
                foreach (var item in product.Photo_Size)
                {
                    PhotoSize_Info ps = new PhotoSize_Info()
                    {
                        Describe = item.Describe,
                        WidthMIN = item.WidthMIN,
                        WidthMAX = item.WidthMAX,
                        HeightMAX = item.HeightMAX,
                        HeightMIN = item.HeightMIN,
                        PageNum = item.PageNum,
                        SizeId = item.SizeId

                    };
                    productEntity.photoSizeInfoList.Add(ps);
                }
                if (product.ProductPrint != null)
                {
                    productEntity.PrintName = product.ProductPrint.PrintSizeName;
                }
                else
                    productEntity.PrintName = "";
            }
            return productEntity;
        }


        /// <summary>
        /// 是否已经存在这一尺寸
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="photosize"></param>
        /// <returns></returns>
        //public bool ExistSize(int productId,PhotoSize_Info photosize)
        //{
        //    bool ret =false;
        //    using(newSampleYPDBEntities db =new newSampleYPDBEntities())
        //    {
        //        Photo_Size ps =db.Photo_Size.FirstOrDefault(p=>p.Tb_Product.ProuctId ==productId
        //            &&p.HeightMAX==photosize.HeightMAX
        //            &&p.HeightMIN ==photosize.HeightMIN
        //            &&p.WidthMAX ==photosize.WidthMAX
        //            &&p.WidthMIN ==photosize.WidthMIN);
        //        if(ps!=null)
        //        {
        //            ret =true;
        //        }
        //    }
        //    return ret;
        //}

        #endregion


    }
}
